package nsn.message.aggregator.views.content;

import nsn.message.aggregator.model.IEntry;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.part.ViewPart;

/**
 *
 * @author Lukasz Nowak
 * @since 15-03-2011
 *
 */
public class ContentView extends ViewPart {

	/**
	 *
	 */
	public ContentView() {
		// TODO Auto-generated constructor stub
	}

	
    private ISelectionListener _selectionListener;

    // private Text text;
    private Browser browser;

    @Override
    public void createPartControl(final Composite parent) {
        browser = new Browser(parent, SWT.None);
        getSite().getPage().addSelectionListener(getSelectionListener());

    }

    @Override
    public void setFocus() {
        // TODO Auto-generated method stub
    }

    /**
     * @return ISelectionListener
     */
    private ISelectionListener getSelectionListener() {
        if (_selectionListener == null) {
            _selectionListener = new ISelectionListener() {

                @Override
                public void selectionChanged(final IWorkbenchPart part,
                        final ISelection selection) {

                    if (selection instanceof IStructuredSelection) {
                        handleSourceSelection(((IStructuredSelection) selection)
                                .getFirstElement());
                    }

                }
            };
        }
        return _selectionListener;
    }

    private void handleSourceSelection(final Object obj) {
        if (obj instanceof IEntry) {
            browser.setText(((IEntry) obj).getContent());
        }

    }

}
